﻿using AutoMapper;
using MediatR;
using SupplyChain.Domain.Entities;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.Goods;

namespace SupplyChain.Write.API.Application.CommandHandler.Goods
{
    public class AddGoodsHandler : IRequestHandler<AddGoodsCommand, bool>
    {
        private readonly IBaseRepository<GoodsInfo> repository;
        private readonly IMapper mapper;

        public AddGoodsHandler(IBaseRepository<GoodsInfo> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }
        public async Task<bool> Handle(AddGoodsCommand request, CancellationToken cancellationToken)
        {
            GoodsInfo goods = mapper.Map<GoodsInfo>(request);
            bool result = await repository.AddAsync(goods);
            return await Task.FromResult(result);
        }
    }
}
